package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2024-01-17
 * Time: 13:55
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先拿到会话对象
        HttpSession session = req.getSession(false);
        if (session == null){
            resp.setContentType("test/html;charset=utf8");
            resp.getWriter().write("您尚未登录");
            return;
        }

        //再将回话中的user Attribut属性删除第掉
        session.removeAttribute("user");
        //跳转到登录页面
        resp.sendRedirect("login.html");
    }
}
